package com.engine.salary.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.extemp.dto.ExtEmpDTO;
import com.engine.salary.entity.extemp.param.ExtEmpImportParam;
import com.engine.salary.entity.extemp.param.ExtEmpQueryParam;
import com.engine.salary.entity.extemp.param.ExtEmpSaveParam;
import com.engine.salary.entity.extemp.po.ExtEmpPO;
import com.engine.salary.util.ResponseResult;
import com.engine.salary.util.SalaryI18nUtil;
import com.engine.salary.util.page.PageInfo;
import com.engine.salary.wrapper.ExtEmpWrapper;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.Collection;
import java.util.Map;

/**
 * 外部人员
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Slf4j
public class ExtEmpController {

    private ExtEmpWrapper getExtEmpWrapper(User user) {
        return ServiceUtil.getService(ExtEmpWrapper.class, user);
    }

    @POST
    @Path("/listPage")
    @Produces(MediaType.APPLICATION_JSON)
    public String list(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ExtEmpQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<ExtEmpQueryParam, PageInfo<ExtEmpDTO>>(user).run(getExtEmpWrapper(user)::listPage, param);
    }

    @POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String save(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ExtEmpSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<ExtEmpSaveParam, String>(user).run(getExtEmpWrapper(user)::save, param);
    }

    @POST
    @Path("/update")
    @Produces(MediaType.APPLICATION_JSON)
    public String update(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ExtEmpSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<ExtEmpSaveParam, String>(user).run(getExtEmpWrapper(user)::update, param);
    }

    @POST
    @Path("/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String delete(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody Collection<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Collection<Long>, String>(user).run(getExtEmpWrapper(user)::delete, ids);
    }

    @GET
    @Path("/detail")
    @Produces(MediaType.APPLICATION_JSON)
    public String save(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "id")  Long id) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, ExtEmpPO>(user).run(getExtEmpWrapper(user)::detail, id);
    }

    // **********************************非系统人员导入 start*********************************/
    //导出导入模板
    @GET
    @Path("/importtemplate/export")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response exportImportTemplate(@Context HttpServletRequest request, @Context HttpServletResponse response) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            XSSFWorkbook workbook = getExtEmpWrapper(user).exportImportTemplate();
            String time = LocalDate.now().toString();
            String fileName = SalaryI18nUtil.getI18nLabel(0, "非系统人员导入模板") + time;
            try {
                fileName = URLEncoder.encode(fileName + ".xlsx", "UTF-8").replaceAll("\\+", "%20");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            StreamingOutput output = outputStream -> {
                workbook.write(outputStream);
                outputStream.flush();
            };
            response.setContentType("application/octet-stream");
            return Response.ok(output).header("Content-disposition", "attachment;filename=" + fileName).header("Cache-Control", "no-cache").build();
        } catch (Exception e) {
            log.error("非系统人员导入模板导出异常", e);
            throw e;
        }

    }

    //导入非系统人员
    @POST
    @Path("/importExtEmp")
    @Produces(MediaType.APPLICATION_JSON)
    public String importExtEmp(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ExtEmpImportParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<ExtEmpImportParam, Map<String, Object>>(user).run(getExtEmpWrapper(user)::importExtEmp, param);
    }

    @POST
    @Path("/preview")
    @Produces(MediaType.APPLICATION_JSON)
    public String previewImportExtEmp(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody ExtEmpImportParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<ExtEmpImportParam, Map<String, Object>>(user).run(getExtEmpWrapper(user)::previewImportExtEmp, param);
    }
    // **********************************非系统人员导入 end*********************************/
}
